package com.ziyun.erp.modules.quoteAttribute.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ziyun.erp.common.e.quoteAttribute.QuoteAttributeValueEnum;
import com.ziyun.erp.modules.quoteAttribute.dao.QuoteAttributeDao;
import com.ziyun.erp.modules.quoteAttribute.entity.QuoteAttributeEntity;
import com.ziyun.erp.modules.quoteAttribute.service.QuoteAttributeService;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("quoteAttributeService")
public class QuoteAttributeServiceImpl extends ServiceImpl<QuoteAttributeDao, QuoteAttributeEntity> implements QuoteAttributeService {

    @Autowired
    private QuoteAttributeDao quoteAttributeDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<QuoteAttributeEntity> page = this.selectPage(
                new Query<QuoteAttributeEntity>(params).getPage(),
                new EntityWrapper<QuoteAttributeEntity>().eq("attribute_type", "normal").eq("del_flag", "false")
                        .like(params.get("attributeName") != null, "attribute_name", (String) params.get("attributeName"))
        );

        return new PageUtils(page);
    }

    @Override
    public int save(QuoteAttributeEntity quoteAttribute) {
        //查询该属性名是否已经存在
        Integer count = this.baseMapper.selectCount(new EntityWrapper<QuoteAttributeEntity>().eq("attribute_type_value", quoteAttribute.getAttributeTypeValue()));
        if (count > 0) {
            throw new RRException("该属性已存在!");
        }
        for (QuoteAttributeValueEnum e : QuoteAttributeValueEnum.values()) {
            if (e.name().equals(quoteAttribute.getAttributeTypeValue())) {
                quoteAttribute.setAttributeName(e.toDescribe());// 设置属性名称
            }
        }
        quoteAttribute.setCreateDate(new Date());
        quoteAttribute.setDelFlag(false);
        quoteAttribute.setAttributeType("normal");
        return this.baseMapper.insert(quoteAttribute);
    }

    @Override
    public R deleteByIds(Integer[] ids) {
        int i = quoteAttributeDao.deleteByIds(ids);
        if (i <= 0) {
            return R.error("删除失败");
        }
        return R.ok();
    }


    /**
     * 列表--工艺
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageCraft(Map<String, Object> params) {
        Page<QuoteAttributeEntity> page = this.selectPage(
                new Query<QuoteAttributeEntity>(params).getPage(),
                new EntityWrapper<QuoteAttributeEntity>().eq("attribute_type", "craft").eq("del_flag", "false")
                        .like(params.get("attributeName") != null, "attribute_name", (String) params.get("attributeName"))
        );

        return new PageUtils(page);
    }

    /**
     * 新增--工艺
     *
     * @param
     * @return
     */
    @Override
    public int saveCraft(QuoteAttributeEntity quoteAttribute) {
        //查询该属性名是否已经存在
        Integer count = this.baseMapper.selectCount(new EntityWrapper<QuoteAttributeEntity>().eq("attribute_type_value", quoteAttribute.getAttributeTypeValue()));
        if (count > 0) {
            throw new RRException("该属性已存在!");
        }
        for (QuoteAttributeValueEnum e : QuoteAttributeValueEnum.values()) {
            if (e.name().equals(quoteAttribute.getAttributeTypeValue())) {
                quoteAttribute.setAttributeName(e.toDescribe());// 设置属性名称
            }
        }
        quoteAttribute.setCreateDate(new Date());
        quoteAttribute.setDelFlag(false);
        quoteAttribute.setAttributeType("craft");
        return this.baseMapper.insert(quoteAttribute);
    }

    /**
     * 列表--材料
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageMaterial(Map<String, Object> params) {
        Page<QuoteAttributeEntity> page = this.selectPage(
                new Query<QuoteAttributeEntity>(params).getPage(),
                new EntityWrapper<QuoteAttributeEntity>().eq("attribute_type", "material").eq("del_flag", "false")
                        .like(params.get("attributeName") != null, "attribute_name", (String) params.get("attributeName"))
        );

        return new PageUtils(page);
    }

    /**
     * 新增--工艺
     *
     * @param
     * @return
     */
    @Override
    public int saveMaterial(QuoteAttributeEntity quoteAttribute) {
        //查询该属性名是否已经存在
        Integer count = this.baseMapper.selectCount(new EntityWrapper<QuoteAttributeEntity>().eq("attribute_type_value", quoteAttribute.getAttributeTypeValue()));
        if (count > 0) {
            throw new RRException("该属性已存在!");
        }
        for (QuoteAttributeValueEnum e : QuoteAttributeValueEnum.values()) {
            if (e.name().equals(quoteAttribute.getAttributeTypeValue())) {
                quoteAttribute.setAttributeName(e.toDescribe());// 设置属性名称
            }
        }
        quoteAttribute.setCreateDate(new Date());
        quoteAttribute.setDelFlag(false);
        quoteAttribute.setAttributeType("material");
        return this.baseMapper.insert(quoteAttribute);
    }

    @Override
    public List<Map<String, Object>> getQuoteGoodsCraftList(String attributeOrigin) {
        List<Map<String, Object>> list = this.baseMapper.selectGoodsCraftConfig(attributeOrigin);
        return list;
    }

}
